package com.elinshaw.netty4.config

import com.elinshaw.protobuf.UserMessage
import io.netty.channel.ChannelHandler
import io.netty.handler.codec.protobuf.ProtobufDecoder
import io.netty.handler.codec.protobuf.ProtobufEncoder
import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender
import io.netty.handler.timeout.IdleStateHandler

import java.util.concurrent.TimeUnit

/**
 * @author Administrator
 */
class NettyServer extends NettyServerTemplate {

    private int port
    private String name

    NettyServer(def port, def name) {
        this.port = port
        this.name = name
    }

    @Override
    protected ChannelHandler[] createHandlers() {
        return [new IdleStateHandler(5, 7, 10, TimeUnit.SECONDS),
                /* new StringDecoder(Charset.forName("utf-8")),
                 new StringEncoder(Charset.forName("utf-8")),*/
                new ProtobufDecoder(UserMessage.User.getDefaultInstance()),
                new ProtobufVarint32LengthFieldPrepender(),
                new ProtobufEncoder(),
                new ServerHandler(),
                new HeartBeatServerHandler()] as ChannelHandler[]
    }

    @Override
    int getPort() {
        return port
    }

    @Override
    String getName() {
        return name
    }

}
